using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LS.GameKit.Behaviour;
using UnityGameFramework.Runtime;

namespace LS.GameKit
{
    public abstract class GameEventBehaviour : GameBehaviourMono
    {
        protected override void OnEnable()
        {
            EventComponent eventComponent = LSEntry.Event;
            if (eventComponent == null) return;
            SubscribeAllEvents(eventComponent);
        }

        protected override void OnDisable()
        {
            EventComponent eventComponent = LSEntry.Event;
            if (eventComponent == null) return;
            UnsubscribeAllEvents(eventComponent);
        }

        protected virtual void SubscribeAllEvents(EventComponent eventComponent) { }
        protected virtual void UnsubscribeAllEvents(EventComponent eventComponent) { }

        public void FireEvent(string key, CommandType? commandType = null, params object[] values)
        {
            var eventModule = LSEntry.Event;
            if (eventModule == null)
                throw new System.NullReferenceException("EventComponent");
            
            var args = GameBehaviourEventArgs.Create<GameBehaviourEventArgs>();
            args.Key = key;
            args.CommandType = commandType;
            args.Values = values;
            eventModule.Fire(this, args);
        }

        public void FireEvent(GameBehaviourEventArgs args)
        {
            var eventModule = LSEntry.Event;
            if (eventModule == null)
                throw new System.NullReferenceException("EventComponent");
            
            if (args == null)
                throw new ArgumentNullException();

            eventModule.Fire(this, args);
        }

        public void FireEventNow(string key, CommandType? commandType = null, params object[] values)
        {
            var eventModule = LSEntry.Event;
            if (eventModule == null)
                throw new System.NullReferenceException("EventComponent");
            
            var args = GameBehaviourEventArgs.Create<GameBehaviourEventArgs>();
            args.Key = key;
            args.CommandType = commandType;
            args.Values = values;
            eventModule.FireNow(this, args);
        }

        public void FireEventNow(GameBehaviourEventArgs args)
        {
            var eventModule = LSEntry.Event;
            if (eventModule == null)
                throw new System.NullReferenceException("EventComponent");
            
            if (args == null)
                throw new ArgumentNullException();

            eventModule.FireNow(this, args);
        }
    }
}
